import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/screen_util.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
import 'package:study_online/entity/MyCourseItem.dart';
import 'package:study_online/global/global.dart';
import 'package:study_online/pages/studyPage/views/CourseItem.dart';
import 'package:study_online/pages/studyPage/views/RequiredCourseTab.dart';

import '../StudyModel.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: StudyPage(),
    );
  }
}

class StudyPage extends StatefulWidget {
  @override
  _StudyPageState createState() => _StudyPageState();
}

class _StudyPageState extends State<StudyPage> with AutomaticKeepAliveClientMixin{

  List<Tab> _tabs = [
    Tab(
      text: "必修课",
    ),
    Tab(
      text: "付费课",
    ),
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
      BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: MediaQuery.of(context).size.height),
      designSize: Size(1080, 2340),
    );

      return SafeArea(
        child: DefaultTabController(
          length: _tabs.length,
          child: Scaffold(
            body: NestedScrollView(
              headerSliverBuilder:
                  (BuildContext context, bool innerBoxIsScrolled) {
                return [
                  // SliverOverlapAbsorber(
                  //   handle:
                  //       NestedScrollView.sliverOverlapAbsorberHandleFor(context),
                  //   sliver: SliverAppBar(
                  //     backgroundColor: Color(0xfff1f1f1),
                  //     forceElevated: innerBoxIsScrolled,
                  //     bottom: PreferredSize(
                  //         child: Container(),
                  //         preferredSize: Size.fromHeight(44)),
                  //     flexibleSpace: Column(
                  //       children: <Widget>[
                  //         Container(
                  //           color: Colors.amber,
                  //           height: 100,
                  //         ),
                  //       ],
                  //     ),
                  //   ),
                  // ),
                  SliverPersistentHeader(
                    delegate: _SliverAppBarDelegate(
                      TabBar(
                        tabs: _tabs,
                        isScrollable: true,
                        indicatorSize: TabBarIndicatorSize.label,
                        labelColor: Colors.black87,
                        unselectedLabelColor: Colors.grey,
                      ),
                    ),
                    pinned: true,
                  ),
                ];
              },
              body: TabBarView(
                children: [
                  RequiredCourseTab(),
                  RequiredCourseTab(),
                ],
              ),
            ),
          ),
        ),
      );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}

class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  final TabBar tabBar;

  _SliverAppBarDelegate(this.tabBar);

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    // TODO: implement build
    return Container(
      color: Colors.white,
      child: tabBar,
    );
  }

  @override
  // TODO: implement maxExtent
  double get maxExtent => tabBar.preferredSize.height;

  @override
  // TODO: implement minExtent
  double get minExtent => tabBar.preferredSize.height;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    // TODO: implement shouldRebuild
    return false;
  }
}
